import { Controller, Get, Render, Res, Param } from '@nestjs/common';
import { TUserBasicService } from '../modules/users/user-basic.service';
import { BlogArticleService } from '../service/article.service';
import { TableTemplateService } from '../service/template.service';
import { Response } from 'express';

@Controller()
export class ViewsController {
  constructor(
    private readonly userService: TUserBasicService,
    private readonly bArticleService: BlogArticleService,
    private readonly tTemplateService: TableTemplateService,
  ) {}
  @Get()
  @Render('index') // 渲染 views/index.hbs
  async root() {
    const res = await this.bArticleService.getBlogArticle(13);
    return {
      title: '首页',
      message: ' 22222' + JSON.stringify(res),
      currentYear: new Date().getFullYear(),
    };
  }

  @Get('home')
  @Render('pages/home')
  async home() {
    const res = await this.bArticleService.getBlogArticles();
    return {
      articles: res,
      currentYear: new Date().getFullYear(),
    };
  }

  @Get('login')
  @Render('pages/login')
  login() {
    return { message: '独立页面内容' };
  }

  @Get('apiTools')
  @Render('apiTools')
  apiTools() {
    return { message: '独立页面内容' };
  }

  @Get('user')
  // @Render('user')
  user(@Res() res: Response) {
    return res.render('user', {
      title: '自定义页面',
      layout: 'layouts/main', // 启用布局文件
      message: '这是自定义布局的页面',
    });
  }

  @Get('article/:id')
  @Render('pages/detail')
  async getArticleDetail(@Param('id') id: string) {
    const res = await this.bArticleService.getBlogArticle(+id);
    return {
      title: res.title,
      article: res.content,
      currentYear: new Date().getFullYear(),
    };
  }
  @Get('template')
  @Render('pages/template')
  async template() {
    // const randomChineseWords = ['风景', '山水', '花鸟', '人物', '建筑', '美食', '艺术', '自然', '城市', '夜景'];
    // const randomDescriptions = ['美丽的', '壮观的', '精致的', '优雅的', '独特的', '震撼的', '宁静的', '繁华的', '古典的', '现代的'];
    // const randomTemplates = Array.from({ length: 5 }, (_, index) => ({
    //     pic_description: `${randomDescriptions[Math.floor(Math.random() * randomDescriptions.length)]}${randomChineseWords[Math.floor(Math.random() * randomChineseWords.length)]}照片`,
    //     pic_name: `${randomChineseWords[Math.floor(Math.random() * randomChineseWords.length)]}图片${index + 1}`,
    //     pic_url: `https://picsum.photos/${295 + index}/${331 + index}`,
    //     createTime: new Date(),
    //     type: Math.floor(Math.random() * 3) + 1
    // }));
    // this.tTemplateService.createTemplates(randomTemplates)

    // this.tTemplateService.createTemplate({
    //   pic_description:"666666",
    //   pic_name: 'backd',
    //   pic_url: 'https://picsum.photos/295/331',
    //   createTime: new Date(),
    //   type: 1
    // })

    // this.tTemplateService.createTemplates([
    //   {
    //     pic_description: '666666',
    //     pic_name: 'backd',
    //     pic_url: 'https://picsum.photos/295/331',
    //     createTime: new Date(),
    //     type: 1,
    //   },
    // ]);
    const res = await this.tTemplateService.getTemplates();
    return {
      templates: res,
      message: JSON.stringify(res),
    };
  }
}
